//
//  ZAQHomeTableViewCell.swift
//  ZAQWBDZT
//
//  Created by 赵安庆 on 16/9/24.
//  Copyright © 2016年 zaq. All rights reserved.
//

import UIKit
//  cell子控件之间的内容间距
let HMHomeTableViewCellMargin: CGFloat = 10
//  设置微博内容字体大小
let ZAQHomeTableViewCellFontSize: CGFloat = 14


class ZAQHomeTableViewCell: UITableViewCell {
    

    //  记录当前toolbar顶部约束
    var toolBarTopConstraint: Constraint?

    //  cell需要使用ViewModel
    var statusViewModel: ZAQstatusViewModel? {
        didSet {
            //  可以给cell里面相应的子控件绑定数据
            originalView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel


            //  把toolbar顶部约束卸载掉
            toolBarTopConstraint?.uninstall()

            //  判断其是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博
                //  更新约束,显示转发微博,绑定数据
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })

                retweetView.statusViewModel = statusViewModel
            } else {
                //  没有转发微博
                //  更新约束, 隐藏转发微博视图
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })



            }

//            print(statusViewModel?.status?.pic_urls?.first?.thumbnail_pic)


        }
    }

/**
/Users/mac/ZAQWB/ZAQWBDZT/ZAQWBDZT/Classes/Views/Home/View/ZAQHomeTableViewCell.swift:20:9: Invalid redeclaration of 'statusViewModel'
     */

    //  MARK: --    懒加载控件
    private lazy var originalView: ZAQStatusOriginalView = {
        let view = ZAQStatusOriginalView()
//        view.backgroundColor = RandomColor()
        return view
    }()
    //  toolbar视图
    private lazy var toolBar: ZAQStatusToolBar = ZAQStatusToolBar()

    //  转发微博视图
    private lazy var retweetView: ZAQStatusRetweetView = {
        let view = ZAQStatusRetweetView()
//        view.backgroundColor = RandomColor()
        return view
    }()


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)

        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        //  设置约束
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        toolBar.snp_makeConstraints { (make) in
            toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(retweetView)
            make.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            //  关键约束 -> toolbar的底部约束等于contentView的底部约束
            make.bottom.equalTo(contentView.snp_bottom)

        }


}
}
